<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 注册接口
 * @param 
 *         $nick = 作用
 * 
 * 
 */
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');

class aj_register extends ml_controller 
{
    private $email;
    private $nick;
    private $gender;
    private $password;
    private $validate;
    private $scode;

    function initParam()
    {
        $this->email = $this->input('email' , 'post');
        $this->nick = $this->input('nick' , 'post');
        $this->gender = $this->input('gender' , 'post');
        $this->password = $this->input('password' , 'post');
        $this->validate = $this->input('validate' , 'post');
        $this->scode = $this->input('scode' , 'post');
    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam()
    {
        $this->check_referer();
        //scode
        if(!ml_tool_securecode::check_scode($this->scode))
            $this->api_output(ML_RCODE_HACK);
        //validate
        $oSession = $this->getSession();
        $vcode_sess = $oSession->getVal('verify_default');
        $oSession->unregister('verify_default');
        $oSession->save();
        if(!ml_tool_securecode::check_validate_code($this->validate , $vcode_sess))
            $this->api_output(ML_RCODE_PARAM , array('result' => 'vcode') , '');
        
        //昵称
        if(!preg_match(ML_DATARULE_USER_NICK_PREG , $this->nick))
            $this->api_output(ML_RCODE_PARAM);

         /**
         * @todo 待加敏感词过滤
         */
        if(!Tool_keyword::check_article_kill_keyword($this->nick,'utf8')){
           return $this->api_output(ML_RCODE_SUCC , array('result'=>false));
        }    
        if(!ml_tool_keyword::haveBasicKillWord($this->nick))
            $this->api_output(ML_RCODE_SUCC , array('result'=>false));
                
        $len = Tool_string::str_width($this->nick);
        if($len < ML_DATARULE_USER_NICK_MINLEN || $len > ML_DATARULE_USER_NICK_MAXLEN)
            $this->api_output(ML_RCODE_PARAM);
        
        //email
        if(filter_var($this->email , FILTER_VALIDATE_EMAIL) == false)
            $this->api_output(ML_RCODE_PARAM);
            
        $len = strlen($this->email);
        if($len < ML_DATARULE_USER_EMAIL_MINLEN || $len > ML_DATARULE_USER_EMAIL_MAXLEN)
            $this->api_output(ML_RCODE_PARAM);
            
        //gender
        $this->gender = ML_DATARULE_USER_GENDER_GIRL;
        
        //password
        if(!preg_match(ML_DATARULE_USER_PASSWORD_PREG , $this->password))
            $this->api_output(ML_RCODE_PARAM);
        
        
        //关键字
//        if(ml_tool_keyword::haveBasicKillWord($this->nick))
//            $this->api_output(ML_RCODE_PARAM , array('result' => 'keyword'));
//            
            
        //检查权限
        if(!$this->check_permission(ML_PERMISSION_LOGOUT_ONLY)) {
            $this->api_output(ML_RCODE_HACK);
        }

    }
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main()
    {
        $oAccount = new ml_model_dbUserAccount();
        
        //
        if(!$oAccount->isNickExists($this->nick , $isExists))
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        elseif($isExists)
            $this->api_output(ML_RCODE_PARAM , array('result' => 'nick_exists'));
            
        if(!$oAccount->isEmailExists($this->email , $isExists))
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        elseif($isExists)
            $this->api_output(ML_RCODE_PARAM , array('result' => 'email_exists'));
            
        //开始注册流程
        $arrAccount = array(
            'nickname' => $this->nick,
            'password' => $this->password,
            'email' => $this->email,
        );
        if(!$oAccount->createAccount($arrAccount))
        {
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
        
        $uid = $oAccount->insert_id();
        //行为日志
        ml_tool_actionlog::register($uid , $this->email , $this->nick , $this->gender);
        //投放队列
        ml_tool_sendMq::completeRegister($uid);
        //搜索队列，是否可以放到completeRegister
        ml_tool_sendMq::search_user('A', $uid);
        
        //投昵称队列        
        $arr = array('uid'=>$uid,'nick'=>$this->nick);
        ml_tool_sendMq::update_nick($arr);

        $follow_data=array(
            'uid'=>$uid,
        );
        ml_tool_sendMq::add_follow_meila($follow_data);


        $title = $this->nick.'，欢迎来到美啦！';
        
        $obj = new ml_tool_securecode();
        $code = ml_tool_securecode::passport_encrypt($this->email);
        
        $act_url = SITE_ROOT_URL.'/page/user/active_register.php?code='.$code;
        
        ml_tool_sendMq::sendMail($this->email , $title , 'register' ,$act_url, $this->nick);
        
        //登录
        unset($arrAccount['password']);
        $arrAccount['uid'] = $uid;
        $arrAccount['status'] = ML_USERSTATUS_OK;
        $arrAccount['verify_email'] = ML_NOTVERIFY;
        $this->loginProxy(ml_login, $arrAccount);
        //设置cookie
        $this->_login->cookie_remember($arrAccount['uid'], $arrAccount['email']);
        
        $this->api_output(ML_RCODE_SUCC);
    }
}

new aj_register();
